package com.yuelan.apartment.controller;

import com.yuelan.apartment.domain.ApaTenant;
import com.yuelan.apartment.domain.dto.ApaTenantDto;
import com.yuelan.apartment.domain.vo.ApaTenantVo;
import com.yuelan.apartment.service.ApaTenantService;
import com.yuelan.common.annotation.Log;
import com.yuelan.common.core.controller.BaseController;
import com.yuelan.common.core.domain.AjaxResult;
import com.yuelan.common.core.page.TableDataInfo;
import com.yuelan.common.enums.BusinessType;
import com.yuelan.common.utils.poi.ExcelUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;


/**
 * @description: 租户管理
 * @Author: ZhaoYi
 * @date: 24-5-23 17:01
 */

@RestController
@RequestMapping(value = "/tenant")
public class ApaTenantController extends BaseController {

    @Resource
    private ApaTenantService tenantInfoService;


    @PostMapping("/export")
    @PreAuthorize("@ss.hasAnyRoles('admin,common')")
    @Log(title = "导出房客信息", businessType = BusinessType.EXPORT)
    public void export(HttpServletResponse response) {
        List<ApaTenantVo> apaTenantVoList = tenantInfoService.exportData();
        ExcelUtil<ApaTenantVo> util = new ExcelUtil<>(ApaTenantVo.class);
        util.exportExcel(response, apaTenantVoList , "房客信息数据");
    }


    @PostMapping("/add")
    @PreAuthorize("@ss.hasAnyRoles('admin,common')")
    @Log(title = "房客信息录入", businessType = BusinessType.INSERT)
    public AjaxResult addTenant(@RequestBody @Valid ApaTenantDto tenantDto){
        return toAjax(tenantInfoService.add(tenantDto));
    }


    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasAnyRoles('admin,common')")
    @Log(title = "房客退租", businessType = BusinessType.DELETE)
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tenantInfoService.deleteTenantByIds(ids));
    }


    @PutMapping("/edit")
    @PreAuthorize("@ss.hasAnyRoles('admin,common')")
    @Log(title = "房客信息更新", businessType = BusinessType.UPDATE)
    public AjaxResult updateTenant(@RequestBody ApaTenant apaTenant) {
        return toAjax(tenantInfoService.update(apaTenant));
    }


    @GetMapping("/load")
    @PreAuthorize("@ss.hasAnyRoles('admin,common')")
    public AjaxResult load(Long id) {
        ApaTenant load = tenantInfoService.load(id);
        return success(load);
    }


    @GetMapping("/pageList")
    @PreAuthorize("@ss.hasAnyRoles('admin,common')")
    public TableDataInfo pageList(ApaTenant apaTenant) {
        startPage();
        return tenantInfoService.page(apaTenant);
    }


    @DeleteMapping("/lose")
    @PreAuthorize("@ss.hasAnyRoles('admin,common')")
    @Log(title = "房客退租", businessType = BusinessType.DELETE)
    public AjaxResult lose(Long id) {
        return null;
    }

}
